<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    {% load static %}
    <script src="/static/js/axios.min.js"></script>
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/layui/layui.js"></script>
    <link href="/static/layui/css/layui.css" rel="stylesheet">

    <style>
        input {
            width: 60%;
            height: 30px;
            margin: 10px;
        }

        hr {
            width: 80%;
            opacity: 0.3;
        }

        .login-box {
            width: 400px;
            height: 400px;
            border-radius: 10px;
            background-color: aliceblue;
            left: 50%;
            position: absolute;
            transform: translateX(-50%);

        }

        .box-title {
            text-align: center;
        }

        .box-content {
            text-align: center;
        }


    </style>
</head>
<body>
<div class="login-box">
    <div class="box-title">
        <h2>注册</h2>
    </div>
    <hr/>
    <div class="box-content layui-form">
        <p>
            <input type="text" value="" placeholder="请输入用户名" id="username">
        </p>
        <p>
            <input type="password" value="" placeholder="请输入密码" id="password">
        </p>
        <p>
            <input type="password" value="" placeholder="请再次输入密码" id="password2">
        </p>
        <p>
            <input type="button" value="注册" class="layui-btn layui-btn-normal" onclick="register()">
            <input type="button" value="取消" class="layui-btn layui-btn-danger">
        </p>
    </div>

</div>
<script>
    layui.use('layer');
    var layer = layui.layer;
    const register = async () => {
        var password = $('#password').val();
        var password2 = $('#password2').val();
        var user = $('#username').val();

        if (user && password && password2) {

            if (password === password2) {
                var username = document.getElementById("username").value;
                var data = new FormData();
                data.append('username', username);
                data.append('password', password);
                let res = await axios.post('/user/reg/', data)

                if (res.data.code === 200) {
                    layer.msg(res.data.msg);
                    setTimeout(window.location.href = '/user/login/', 2000)
                } else {
                    layer.msg(res.data.msg);
                }
            } else {
                layer.msg("请确保两次输入的密码一致")
            }
        } else {
            layer.msg("用户名或密码不能为空")
        }
    }

</script>

</body>
</html>